/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **    Start/Stop all session server
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.GRStatus;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.GRStatusHelper;
import com.hp.snap.gr.utils.SSHTools;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 
 * Start/Stop all session server.The operation type(start/stop) configured in the action, and executing shell script to
 * complete the operation.
 * 
 * There are two parameter can be configured in the action: Shell and OperationType.
 * Shell: Start/Stop all session server shell script.
 * OperationType: mark the oprtion is start or stop, optional value is: StartAll or StopAll.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class SessionServerShellOperationActionHandler extends AbstractActionHandler {

    private final static String START_ALL = "StartAll";
    private final static String STOP_ALL = "StopAll";
    private static Logger logger = LoggerFactory
            .getLogger(SessionServerShellOperationActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {


        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);
        String opType = parameterMap.get("OperationType");
        if (opType != null && opType.equals(START_ALL))
            logger.info("Starting all session servers, please checking the session server logs ......");
        else if (opType != null && opType.equals(STOP_ALL))
            logger.info("Stopping all session servers, please checking the session server logs ......");

        String shellscript = parameterMap.get("Shell");

        String localSiteName = Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME);
        String grIP = Utils.getGRHostIP(localSiteName);
        String sshUser = Utils.getConfigurationValue(Constants.CFG_GRG_ROOT + "/" + localSiteName, Constants.CFG_GRG_SITE_SSH_USER);
        String result = "";
        SSHTools ssh = new SSHTools(sshUser, Constants.SSH_PATH + File.separator + Constants.SSH_KEY_FILE);
        try {
            result = ssh.executeRemoteShell(false, shellscript, grIP);
        } catch (Exception e) {
            throw new ActionException("Connect master session server failed!", e.getCause());
        }
        int status = ssh.getExitStatusOfCommandResult(result);
//        String resultStr = ssh.getCommandOutput(result);
        if (status != 0) {
            if(logger.isDebugEnabled()){
                logger.debug("Set the Site status is {}",GRStatus.ERROR);
            }
            GRStatusHelper.updateStatus(GRStatus.ERROR);
            logger.error("execute the getting session server status shell script failed!");
            throw new ActionException("execute the getting session server status shell script failed!");
        }
    }

}
